﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ShenRuQianChuChapt6Binding
{
    /// <summary>
    /// chapt6_3_10UseLINQToRetrieveResultsAsTheSourceOfBinding.xaml 的交互逻辑
    /// </summary>
    public partial class chapt6_3_10UseLINQToRetrieveResultsAsTheSourceOfBinding : Window
    {
        public chapt6_3_10UseLINQToRetrieveResultsAsTheSourceOfBinding()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //准备数据源
            List<Student> stuList = new List<Student>
            {
                new Student(){ Id = 0,Name="Tim", Age=29},
                new Student(){ Id = 1,Name="Tom", Age=28},
                new Student(){ Id = 2,Name="Kyle", Age=27},
                new Student(){ Id = 3,Name="Tony", Age=26},
                new Student(){ Id = 4,Name="Vina", Age=25},
                new Student(){ Id = 5,Name="Mike", Age=24},
            };

            this.listViewStudents.ItemsSource =
                from stu in stuList
                where stu.Name.StartsWith("T")
                select stu;
        }

        private void ButtonDataTable_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = this.Load();

            this.listViewStudents.ItemsSource =
                from row in dt.Rows.Cast<DataRow>()
                where Convert.ToString(row["Name"]).StartsWith("T")
                select new Student()
                {
                    Id = int.Parse(row["Id"].ToString()),
                    Name = row["Name"].ToString(),
                    Age = int.Parse(row["Age"].ToString()),
                };            
        }

        //返回一个数据表对象
        private DataTable Load()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "Tim";
            dr[2] = "29";
            dt.Rows.Add(dr);

            DataRow dr1 = dt.NewRow();
            dr1[0] = 2;
            dr1[1] = "Tom";
            dr1[2] = "28";
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr2[0] = 3;
            dr2[1] = "Tony";
            dr2[2] = "27";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3[0] = 4;
            dr3[1] = "Kyle";
            dr3[2] = "26";
            dt.Rows.Add(dr3);

            DataRow dr4 = dt.NewRow();
            dr4[0] = 5;
            dr4[1] = "Vian";
            dr4[2] = "25";
            dt.Rows.Add(dr4);

            DataRow dr5 = dt.NewRow();
            dr5[0] = 6;
            dr5[1] = "Emily";
            dr5[2] = "24";
            dt.Rows.Add(dr5);

            return dt;
        }
    }
}

